package com.farah.lease.web.admin.service.impl;


import com.farah.lease.common.exception.LeaseException;
import com.farah.lease.common.result.ResultCodeEnum;
import com.farah.lease.common.utils.JwtUtil;
import com.farah.lease.model.entity.SystemUser;
import com.farah.lease.model.entity.UserInfo;
import com.farah.lease.model.enums.BaseStatus;
import com.farah.lease.web.admin.mapper.SystemUserMapper;
import com.farah.lease.web.admin.mapper.UserInfoMapper;
import com.farah.lease.web.admin.service.LoginService;
import com.farah.lease.web.admin.vo.login.CaptchaVo;
import com.farah.lease.web.admin.vo.login.LoginVo;
import com.farah.lease.web.admin.vo.system.user.SystemUserInfoVo;
import com.wf.captcha.SpecCaptcha;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import com.farah.lease.common.constant.RedisConstant;
import org.springframework.util.StringUtils;

import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * @Author 不爱跑步的法拉韶
 * @Date 2024-06-21__11:56
 **/
@Service
public class LoginServiceImpl implements LoginService {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private SystemUserMapper systemUserMapper;

    /**
     * 获取图片验证码
     * @return 一个图片的信息
     */
    @Override
    public CaptchaVo getCaptcha() {
        SpecCaptcha specCaptcha = new SpecCaptcha(130, 48, 4);
        String code = specCaptcha.text().toLowerCase();
        String key = RedisConstant.ADMIN_LOGIN_PREFIX + UUID.randomUUID();
        stringRedisTemplate.opsForValue().set(key, code,RedisConstant.ADMIN_LOGIN_CAPTCHA_TTL_SEC, TimeUnit.SECONDS);
        return new CaptchaVo( specCaptcha.toBase64(), key);
    }

    @Override
    public String login(LoginVo loginVo) {
        //1.判断是否输入了验证码
        if (!StringUtils.hasText(loginVo.getCaptchaCode())) {
            // 验证码为空
          throw  new LeaseException(ResultCodeEnum.ADMIN_CAPTCHA_CODE_NOT_FOUND);
        }
        //2.从redis中获取验证码的值
        String redisCode = stringRedisTemplate.opsForValue().get(loginVo.getCaptchaKey());
        if(redisCode==null){
            //验证码过期
           throw new LeaseException(ResultCodeEnum.ADMIN_CAPTCHA_CODE_EXPIRED);
        }
        //3.判断验证码是否正确
        if(!redisCode.equals(loginVo.getCaptchaCode().toLowerCase())){
            //验证码错误
          throw new LeaseException(ResultCodeEnum.ADMIN_CAPTCHA_CODE_ERROR);
        }
        //4.判断用户是否存在
        SystemUser user = systemUserMapper.selectOneByUsername(loginVo.getUsername());
        if(user==null){
            //用户不存在
           throw new LeaseException(ResultCodeEnum.ADMIN_ACCOUNT_NOT_EXIST_ERROR);
        }
        //5.判断用户状态是否被禁用
        if(user.getStatus()== BaseStatus.DISABLE){
            //用户被禁用
            throw new LeaseException(ResultCodeEnum.ADMIN_ACCOUNT_DISABLED_ERROR);
        }
        //密码加密
        loginVo.setPassword(DigestUtils.md5Hex(loginVo.getPassword()));
        //6.判断密码是否正确
        if(!user.getPassword().equals(loginVo.getPassword())){
            //密码错误
            throw new LeaseException(ResultCodeEnum.ADMIN_ACCOUNT_ERROR);
        }
        return JwtUtil.createToken(user.getId(), user.getUsername());
    }

    @Override
    public SystemUserInfoVo getByidUserInfo(Long userId) {
        SystemUser user = systemUserMapper.selectById(userId);
        SystemUserInfoVo userInfoVo = new SystemUserInfoVo();
        userInfoVo.setName(user.getName());
        userInfoVo.setAvatarUrl(user.getAvatarUrl());
        return userInfoVo;
    }
}
